var host = 'https://api.mnancheng.com/index.php/'
// var host = 'http://10.0.0.216:8080/';
var bocai = 'https://www.forzadata.cn/api/'
// var bocai = "http://10.0.0.216:8080/api/"
// import {sort_ASCII} from'../app'
import md5 from './md5.js';
var requestUtil = (function () {
  function sort_ASCII(obj) {
    var arr = new Array();
    var num = 0;
    for (var i in obj) {
      arr[num] = i;
      num++;
    }
    var sortArr = arr.sort();
    var sortObj = {};
    for (var i in sortArr) {
      sortObj[sortArr[i]] = obj[sortArr[i]];
    }
    var tem = "";
    for (var i in sortObj) {
      tem += i + '=' + sortObj[i] + "&";
    }
    tem += 'key=' + 'jfio8as224fhhijfsddidkkjenwe5ernp'
    var sign = md5(tem).toUpperCase()
    return sign;
  }

  function putRequest(url, param) {
    return basicRequest('PUT', host + url, param)
  }

  function getRequest(url, showErrorModal = true, hideLoad = true) {
    return basicRequest('GET', host + url, null, showErrorModal, hideLoad)
  }

  function postRequest(url, param, hideInfo = true, showErrorModal = true) {
    let unionid = wx.getStorageSync('openid')
    param = {
      ...param,
      unionid
  
    }
    let sign = sort_ASCII(param)

    return basicRequest('POST', host + url, {
      sign,
      ...param
    }, showErrorModal, hideInfo)
  }

  function bocaiputReq(url, param) {
    return basicRequest('PUT', bocai + url, param)
  }

  function bocaigetReq(url, showErrorModal = true, hideLoad = true) {
    return basicRequest('GET', bocai + url, null, showErrorModal, hideLoad)
  }

  function bocaipostReq(url, param, hideInfo = true) {
    return basicRequest('POST', bocai + url, param, true, hideInfo)
  }

  function basicRequest(
    method,
    url,
    param = null,
    showErrorModal = true,
    hideInfo = true
  ) {
    var token = getApp().globalData.token
    var header = {
      'content-type': 'application/json'
    }
    if (url.indexOf('forzadata') < 0) {
      if (token) {
        header.WXAPPCHATID = token
      } else if (wx.getStorageSync('token')) {
        header.WXAPPCHATID = wx.getStorageSync('token')
      }
    }
    return new Promise(function (resolve, reject) {
      console.log(url);
      wx.showLoading({
        title: '加载中',
      })
      wx.request({
        url: url,
        data: param ? param : {},
        header: header,
        method: method,
        success: function (res) {
          wx.hideLoading()
          if (res.data.code == 200) {
            resolve(res.data.data?res.data.data:res.data)
          } else {
            reject(res)
            wx.showToast({
              title: res.data.msg,
              icon: 'none',
              duration:4000
            })
          }
        },
        fail: (res) => {
          wx.hideLoading()
          console.log(res)
          // token不对处理
          reject(res)
          wx.showModal({
            title: JSON.stringify(res),
            content: JSON.stringify(res) + '结果'
          })
        },
        complete: (err) => {
          setTimeout(() => {
            wx.hideLoading()
          }, 4000);
         
        }
      })
    })
  }
  return {
    getRequest: getRequest,
    postRequest: postRequest,
    putRequest: putRequest,
    host: host,
    bocai,
    bocaiputReq,
    bocaigetReq,
    bocaipostReq,
  }
})()
var login = function () {}
module.exports = requestUtil